package activiti;

import org.activiti.engine.*;
import org.activiti.engine.identity.User;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Attachment;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Raih
 * @version 1.0
 * @date 2020/5/11 11:49
 *
 * 第六次实验代码
 *
 */
public class Sixth2 {

    public static void main(String[] args) throws ParseException, FileNotFoundException {

//        ProcessEngine pe = ProcessEngines.getDefaultProcessEngine();
//        // 获取task实例，以及设置相关信息
//        TaskService ts= pe.getTaskService();
//        Task task1=ts.newTask("001");
//        task1.setName("task1");
//        task1.setDescription("001");
//        ts.saveTask(task1);
//        System.out.println("task1 ok");
//        Task task2=ts.newTask("002");
//        task2.setName("task2");
//        task2.setDescription("002");
//        ts.saveTask(task2);
//        System.out.println("task2 ok");
//        TaskQuery tq=ts.createTaskQuery();
//        List<Task> tasks=tq.orderByTaskPriority().desc().list();
//        for(Task task:tasks) {
//            System.out.println("Task ID:"+task.getId()+",task name:"+task.getName()+
//                    ",priority:"+task.getPriority());
//        }
//        ts.deleteTask(task1.getId());
//        ts.complete(task2.getId());
//        System.out.println("task1 delete");
//        ProcessEngine pe = ProcessEngines.getDefaultProcessEngine();
//        TaskService ts = pe.getTaskService();
//        int taskidmax = 100000, taskidmin = 1;
//        int taskidnum = (int) (Math.random() * (taskidmax - taskidmin) + taskidmin);
//        int max = 100, min = 0;
//        int priority = (int) (Math.random() * (max - min) + min);
//        //创建用户前查询ACT_RU_TASK表
//        System.out.println("Before the 1st Task is created,query the table act_ru_task");
//        queryTasks();
//        //创建任务task1
//        String task1Id = "Task" + String.valueOf(taskidnum);
//        Task task1 = ts.newTask(task1Id);
//        task1.setName("vacationRequest1");
//        task1.setDescription("Vacation Request");
//        task1.setPriority(priority);
//        ts.saveTask(task1);
//        //创建用户后查询ACT_RU_TASK
//        System.out.println("After the 1st Task is created,query the table act_ru_task");
//        queryTasks();
//        //创建用户前查询ACT_RU_TASK表
//        System.out.println("Before the 2nd Task is created,query the table act_ru_task");
//        queryTasks();
//        taskidnum = (int) (Math.random() * (taskidmax - taskidmin) + taskidmin);
//        priority = (int) (Math.random() * (max - min) + min);
//        String task2Id = "Task" + String.valueOf(taskidnum);
//        Task task2 = ts.newTask(task2Id);
//        task2.setName("vacationRequest2");
//        task2.setDescription("Vacation Request");
//        task2.setPriority(priority);
//        ts.saveTask(task2);
//        //创建用户前查询ACT_RU_TASK表
//        System.out.println("After the 2nd Task is created,query the table act_ru_task");
//        queryTasks();
//        //按照任务优先级降序排序
//        System.out.println("Sort the tasks by priority in descending order");
//        TaskQuery tq = ts.createTaskQuery();
//        List<Task> tasks = tq.orderByTaskPriority().desc().list();
//        for (Task task : tasks) {
//            System.out.println("Task ID: " + task.getId() + ",Task Name:" + task.getName() + ",Priority:" +
//                    task.getPriority());
//        }
//        List<Task> qtasks = tq.list();
//        for (Task task : qtasks) {
//            ts.deleteTask(task.getId());
//            ;
//        }
//    }
//
//    static void queryTasks() {
//        ProcessEngine pe = ProcessEngines.getDefaultProcessEngine();
//        TaskService ts = pe.getTaskService();
//        TaskQuery tq = ts.createTaskQuery();
//        List<Task> tasks = tq.list();
//        for (Task task : tasks) {
//            System.out.println("Task ID: " + task.getId() + ",Task Name:" + task.getName() + ",Priority:" +
//                    task.getPriority());
//        }
//    }
        ProcessEngine pe = ProcessEngines.getDefaultProcessEngine();
        RepositoryService res = pe.getRepositoryService();
        RuntimeService rus = pe.getRuntimeService();
        TaskService ts = pe.getTaskService();
        IdentityService is = pe.getIdentityService();
        Deployment dep = res.createDeployment().addClasspathResource("bpmn/BusinessTrip.bpmn").deploy();
        ProcessDefinition pd = res.createProcessDefinitionQuery().deploymentId(dep.getId()).singleResult();
        ProcessInstance pi = rus.startProcessInstanceById(pd.getId());
        //任务查询
        Task queryTask = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        //参数设置
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = dateFormat.parse("2020-04-28 10:00:01");
        Date endDate = dateFormat.parse("2020-04-30 18:00:01");
        String[] vars = {"Start Time", "End Time"};
        Map<String, Object> taskvars = new HashMap<String, Object>();
        taskvars.put(vars[0], startDate);
        taskvars.put(vars[1], endDate);
        ts.setVariables(queryTask.getId(), taskvars);
        User user1 = is.newUser("user001");
        user1.setFirstName("ming");
        user1.setLastName("wang");
        is.saveUser(user1);
        User user2 = is.newUser("user002");
        user2.setFirstName("san");
        user2.setLastName("zhang");
        is.saveUser(user2);
        ts.addCandidateUser(queryTask.getId(), user1.getId());
        ts.addCandidateUser(queryTask.getId(), user2.getId());
        //创建附件
        InputStream ins = new FileInputStream(new File("../resources/1.txt"));
        Attachment att = ts.createAttachment("Local file", queryTask.getId(), pi.getId(), "本地附件",
                "申请出差的附件文件", ins);
        Attachment att1 = ts.createAttachment("Web URL", queryTask.getId(), pi.getId(), "出差申请",
                "通知", "http://news.pdsu.edu.cn/xygg.htm");
        System.out.println("get fujian");
        Attachment queryAtt = ts.getAttachment(att.getId());
        System.out.println("Attachment Type:" + queryAtt.getType() + "Attachment Name:" + queryAtt.getName());
        System.out.println("当前任务的附件");
        List<Attachment> taskAttachments = ts.getTaskAttachments(queryTask.getId());
        for (Attachment attachment : taskAttachments) {
            System.out.println("Attachment Type:" + queryAtt.getType() + "Attachment Name:" + queryAtt.getName());
        }
        //获取流程实例的附件
        System.out.println("当前流程实例的附件");
        List<Attachment> piAttachments = ts.getProcessInstanceAttachments(pi.getId());
        for (Attachment attachment : taskAttachments) {
            System.out.println("Attachment ID:" + attachment.getId() + "Attachment Name:" + attachment.getName());
        }
        ts.complete(queryTask.getId());
        //任务查询
        queryTask = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        ts.addCandidateUser(queryTask.getId(), user1.getId());
        ts.addCandidateUser(queryTask.getId(), user2.getId());
        ts.claim(queryTask.getId(), user2.getId());
        Attachment taskaAtt = ts.createAttachment("Web URL", queryTask.getId(), pi.getId(), "出差申请", "通知", "http://news.pdsu.cn");
        System.out.println("get fujian");
        Attachment queryAtt2 = ts.getAttachment(att.getId());
        System.out.println("Attachment Type:" + queryAtt2.getType() + "Attachment Name:" + queryAtt2.getName());
        System.out.println("当前任务的附件");
        taskAttachments = ts.getTaskAttachments(queryTask.getId());
        for (Attachment attachment : taskAttachments) {
            System.out.println("Attachment Type:" + queryAtt.getType() + "Attachment Name:" + queryAtt.getName());
        }
        //获取流程实例的附件
        System.out.println("当前流程实例的附件");
        piAttachments = ts.getProcessInstanceAttachments(pi.getId());
        for (Attachment attachment : piAttachments) {
            System.out.println("Attachment ID:" + attachment.getId() + "Attachment Name:" + attachment.getName());
        }
        //添加评论
        queryTask = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        ts.addComment(queryTask.getId(), pi.getId(), "疫情期间,目的地不能为平顶山辖区");
        ts.addComment(queryTask.getId(), pi.getId(), "疫情期间,出差不能超过三天");
        List<Comment> comments = ts.getProcessInstanceComments(pi.getId());
        for (Comment comment : comments) {
            System.out.println("CommentFullMessage:" + comment.getFullMessage());
        }
        //删除评论
        ts.deleteComments(queryTask.getId(), pi.getId());
        ts.complete(queryTask.getId());
    }

}
